package com.netty.modbus.dto.request;

import com.netty.modbus.exception.DataConvertException;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 * description: WriteBody <br>
 * @date: 2021/10/8 17:18 <br>
 * @author: lwy <br>
 * @version: 1.0 <br>
 */
public class WriteBody extends RequestBody{
    private final byte[] functionCode={1,16};
    /**写入的数据*/
    private final byte[] data;
    /**写入寄存器数量* */
    private final byte[] quantity;
    /**写入字节数量* */
    private final byte quantity1;
    /**写入寄存器起始地址 * */
    private final byte[] startAddress;

    private String devNumber;

    public String getDevNumber() {
        return devNumber;
    }

    public void setDevNumber(String devNumber) {
        this.devNumber = devNumber;
    }

    /**
     * description: 写入
     * version: 1.0
     * date: 2021/10/18 11:07
     * @author: lwy
     * @param startAddress 写入起始位置
     * @param quantity 写入寄存器数量
     * @param quantity1 写入字节数量
     * @param data 写入数据
     * @return
     */
    public WriteBody(byte[] startAddress, byte[] quantity,byte quantity1,byte[] data) {
        ByteBuf dataBuf= Unpooled.buffer();
        dataBuf.writeBytes(functionCode);
        dataBuf.writeBytes(startAddress);
        dataBuf.writeBytes(quantity);
        dataBuf.writeByte(quantity1);
        dataBuf.writeBytes(data);
        this.data=new byte[dataBuf.readableBytes()];
        this.quantity=quantity;
        this.quantity1=quantity1;
        this.startAddress=startAddress;
        dataBuf.readBytes(this.data);
    }

    /**
     * description: 编码
     * version: 1.0
     * date: 2021/10/9 9:55
     * @author: lwy
     * @param
     * @return io.netty.buffer.ByteBuf
     */
    @Override
    public ByteBuf encode() throws DataConvertException {
        return this.commEncode(this.data);
    }
}
